Ext.onReady(function(){
	Ext.define('ChooseTimeWindow', {
		extend : 'Ext.window.Window',
		title : '选择时间',
		width : 400,
		height : 300,
		modal : true,
		isChooseYear : true,
		initComponent : function()
		{
			var me = this;
			
			var yearStore = Ext.create('Ext.data.Store', {
				fields : ['display', 'value'],
				data : [{
							"display" : "2015年",
							"value" : "1"
						}, {
							"display" : "2016年",
							"value" : "2"
						}, {
							"display" : "2017年",
							"value" : "3"
						}, {
							"display" : "2018年",
							"value" : "4"
						}, {
							"display" : "2019年",
							"value" : "5"
						}, {
							"display" : "2020年",
							"value" : "6"
						}, {
							"display" : "2021年",
							"value" : "7"
						}, {
							"display" : "2022年",
							"value" : "8"
						}, {
							"display" : "2023年",
							"value" : "9"
						}, {
							"display" : "2024年",
							"value" : "10"
						}, {
							"display" : "2025年",
							"value" : "11"
						}, {
							"display" : "2026年",
							"value" : "12"
						}]
			});
			
			me.yearCom = Ext.create('Ext.form.ComboBox', {
				fieldLabel : '选择年份',
				store : yearStore,
				queryMode : 'local',
				displayField : 'display',
				valueField : 'value',
				margin : '20 0 20 20',
				labelWidth: 80,
				labelAlign:'right',
				allowBlank: false,
				editable : false,
				name : 'type'
			});	
			
			var monthStore = Ext.create('Ext.data.Store', {
				fields : ['display', 'value'],
				data : [{
							"display" : "一月",
							"value" : "01"
						}, {
							"display" : "二月",
							"value" : "02"
						}, {
							"display" : "三月",
							"value" : "03"
						}, {
							"display" : "四月",
							"value" : "04"
						}, {
							"display" : "五月",
							"value" : "05"
						}, {
							"display" : "六月",
							"value" : "06"
						}, {
							"display" : "七月",
							"value" : "07"
						}, {
							"display" : "八月",
							"value" : "08"
						}, {
							"display" : "九月",
							"value" : "09"
						}, {
							"display" : "十月",
							"value" : "10"
						}, {
							"display" : "十一月",
							"value" : "11"
						}, {
							"display" : "十二月",
							"value" : "12"
						}]
			});

			me.startMonthCom = Ext.create('Ext.form.ComboBox', {
						fieldLabel : '开始时间',
						store : monthStore,
						queryMode : 'local',
						displayField : 'display',
						valueField : 'value',
						margin : '20 0 20 20',
						labelWidth: 80,
						labelAlign:'right',
						allowBlank: false,
						editable : false,
						name : 'type'
					});	
			me.endMonthCom = Ext.create('Ext.form.ComboBox', {
				fieldLabel : '结束时间',
				store : monthStore,
				queryMode : 'local',
				displayField : 'display',
				valueField : 'value',
				margin : '20 0 20 20',
				labelWidth: 80,
				labelAlign:'right',
				allowBlank: false,
				editable : false,
				name : 'type'
			});	
			
			this.buttons = [Ext.create('Ext.Button', {
				    text: '确定',
				    width : 100,
				    height : 30,
				    margin : '0, 30, 0, 0',
				    handler: function() {
	        	 		me.doChoose();
				    }
				}),Ext.create('Ext.Button', {
				    text: '取消',
				    width : 100,
				    height : 30,
				    handler: function() {
						me.hide();
				    }
				})]
			if(me.isChooseYear)
			{
				me.items = [me.yearCom, this.buttons];
			}
			else
			{
				me.items = [me.yearCom, me.startMonthCom, me.endMonthCom, this.buttons];
			}
			
			me.callParent();
		},
		doChoose : function()
		{
			var me = this;
			
			var year = me.yearCom.getValue();
			var startMonth = me.startMonthCom.getValue();
			var endMonth = me.endMonthCom.getValue();
			if(Ext.isEmpty(year))
			{
				Ext.Msg.alert('错误', '请选择年份');
				return;
			}
			if(!me.isChooseYear)
			{
				if(Ext.isEmpty(startMonth))
				{
					Ext.Msg.alert('错误', '请选择开始时间');
					return;
				}
				if(Ext.isEmpty(endMonth))
				{
					Ext.Msg.alert('错误', '请选择结束时间');
					return;
				}
				if(endMonth < startMonth)
				{
					Ext.Msg.alert('错误', '结束时间不能小于开始时间');
					return;
				}
			}

			me.hide();
			var startTime = '';
			var endTime = '';
			var yearArray = ['2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022', '2023', '2024', '2025', '2026'];
			if(me.isChooseYear)
			{
				startTime = yearArray[year - 1] + '-01-01 00:00:00';
				endTime = yearArray[year - 1] + '-12-31 00:00:00';
			}
			else 
			{
				startTime = yearArray[year - 1] + '-' + startMonth + '-01 00:00:00';
				if(endMonth == 12)
				{
					endTime = yearArray[year] + '-01-01 00:00:00';	
				}
				else 
				{
					endTime = yearArray[year - 1] + '-' + (endMonth + 1)+ '-01 00:00:00';
				}				
			}
			
			
			var params = {
				startTime : startTime,
				endTime : endTime,
				type : me.isChooseYear ? 1 : 0
			};
			
			Ext.getCmp("kaohe_grid").storeParams = params;
			
			Ext.apply(Ext.getCmp("kaohe_grid").getStore().proxy.extraParams, params);
			Ext.getCmp("kaohe_grid").getStore().reload();
			if(me.isChooseYear)
			{
				Ext.getCmp('typeMsg').setText('当前为年度考核，选择的时间为：' + me.yearCom.getRawValue())
			}
			else 
			{
				Ext.getCmp('typeMsg').setText('当前为月度考核，选择的时间为：' + me.yearCom.getRawValue() + '的' + me.startMonthCom.getRawValue() + '~' + me.endMonthCom.getRawValue())
			}
			
		}
	});
	
	var toolbar = Ext.create('Ext.toolbar.Toolbar', {
		dock : 'top',
		items : [ {
			text : '月度考核',
			xtype : 'button',
			listeners:{
				click: function(){
					Ext.create('ChooseTimeWindow', {
						isChooseYear : false
					}).show();
				}
			}
		},{
			text : '年度考核',
			xtype : 'button',
			listeners:{
				click: function(){
					Ext.create('ChooseTimeWindow').show();
				}
			}
		},{
			text : '导出',
			xtype : 'button',
			listeners:{
				click: function(){
					exportToExcel();
				}
			}
		},{
			text : '导入文件',
			xtype : 'button',
			listeners:{
				click: function(){
					importAttachment(grid);
				}
			}
		},{
			text : '设置',
			xtype : 'button',
			listeners:{
				click: function(){
					location.href = "/sysMgr/jixiao_mgr"
				}
			}
		}]
	});
		
	var store = Ext.create('Ext.data.Store', {
	    fields:['user.Id', 'user.Username', 'type', 'score', 'user.Department.Name', 'user.Flight.Name'],
	    proxy: {
	        type: 'ajax',
	        url: '/assess/getAssessList',
	        reader: {
	            type: 'json',
	            root: 'data.rows'
	        }
	    }
	});
	var grid = Ext.create('Ext.grid.Panel', {
		id: 'kaohe_grid',
		store:store,
		margin : '20 10 20 10',
		storeParams : null,
		columns : [ {
			text : '序号',
			xtype : 'rownumberer',
			width : 40,
			sortable : false
		}, {
			text : '姓名',
			dataIndex : 'user.Username'
		}, {
			text : '考核类型',
			dataIndex : 'type',
			flex:1
		}, {
			text : '考核成绩',
			dataIndex : 'score',
			flex:1
		}, {
			text : '所属部门',
			dataIndex : 'user.Department.Name',
			flex:1
		}, {
			text : '所属班组',
			dataIndex : 'user.Flight.Name',
			flex:1
		},{
			text : '操作',
			width : 100,
			renderer : function(v,metaData, record)
			{
				return '<a href="assess/detail/' + record.data['user.Id'] + '/' + 
						this.storeParams.type +'/' + 
						this.storeParams.startTime + '/' + 
						this.storeParams.endTime+'">查看详情</a>';
			}
		}],
		bbar : Ext.create('Ext.PagingToolbar', {
							store : store,
							border: false,
							displayInfo : true,
							displayMsg : '显示{0} - {1} / 总数 {2}',
							emptyMsg : "没数据"
						})
	});
	

	var typeMsg = Ext.create('Ext.form.Label', {
		id : 'typeMsg',
		margin : '0 0 0 10'
	});
	
	var panel = Ext.create("Ext.panel.Panel", {
		title: '',
		border : false,
		bodyBorder : false,
		layout : 'fit',
		dockedItems : [ toolbar ],
		items : [typeMsg, grid]
	});
	Ext.create('Ext.Viewport', {
		layout : 'fit',
		style : 'background:none;background-color:white;',
		items : [ panel ],
		listeners : {
			afterrender : function()
			{
				var now = new Date();
				var year = now.getFullYear();
				var month = now.getMonth();
				
				var startTime;
				var endTime;
				
				startTime = year + '-' + (month + 1) + '-01 00:00:00';
				if((month + 2) >= 12)
				{
					endTime = (year+1) + '-01-01 00:00:00';	
				}
				else 
				{
					endTime = year + '-' + (month + 2) + '-01 00:00:00';
				}
				
				var params = {
					startTime : startTime,
					endTime : endTime,
					type : 0
				};
				
				Ext.getCmp("kaohe_grid").storeParams = params;
				
				Ext.apply(Ext.getCmp("kaohe_grid").getStore().proxy.extraParams, params);
				Ext.getCmp("kaohe_grid").getStore().reload();
			}
		}
	
	});
	
	
	Ext.define('ImportWindow',{
		extend : 'Ext.window.Window',
		height : 200,
		width : 600,
		modal : true,
		title : '导入考核文件',
		closeAction  : 'hide',
		initComponent : function()
		{
			var me = this;
			var required = '<span style="color:red;font-weight:bold" data-qtip="Required">*</span>';
			
			this.msg = Ext.create('Ext.form.Label', {
				text : '请勿上传相同文件，若文件名相同，将不会增加你的考核分数'
			});
			
			var typeStore = Ext.create('Ext.data.Store', {
				fields : ['Name', 'Id'],
				proxy: {
			        type: 'ajax',
			        url: '/attach_type/allByType',
			        reader: {
			            type: 'json',
			            root: 'data.rows'
			        },
			        extraParams : {
						'desp' : '考核'
					},
					autoLoad : true
			    }
			});

			typeStore.load();
			this.type = Ext.create('Ext.form.ComboBox', {
						fieldLabel : '考核类别',
						store : typeStore,
						queryMode : 'local',
						displayField : 'Name',
						valueField : 'Id',
						margin : '20 0 20 20',
						labelWidth: 80,
						labelAlign:'right',
						allowBlank: false,
						editable : false,
						name : 'type',
						afterLabelTextTpl: required
					});			
			
			this.form = Ext.create("Ext.form.Panel",{
				border : false,
				margin: '5 5',
				items : [this.msg, this.type, {
					xtype : 'filefield',
					name: "file",
					labelWidth: 100,
					labelAlign:'right',
					fieldLabel: '文件路径',
					buttonText : '选择文件 ...',
					afterLabelTextTpl: required,
					width : 400
				}]
			});			
			
			this.buttons = [Ext.create('Ext.Button', {
							    text: '确定',
							    width : 100,
							    height : 30,
							    margin : '0, 30, 0, 0',
							    handler: function() {
				        	 		me.doUpload();
							    }
							}),Ext.create('Ext.Button', {
							    text: '取消',
							    width : 100,
							    height : 30,
							    handler: function() {
									me.hide();
							    }
							})]
			this.items = [this.form];
			this.callParent();
		},
		doUpload : function()
		{
			var me = this;
			if(me.form.isValid())
			{	
				me.form.submit({
					url : 'assess/importAttachment',
					waitMsg : '上传文件中，请稍等 ...',
					success : function(form, action) {
						if (!action.result.success) {
							Ext.Msg.alert("错误", action.result.msg);
						}
						else{
							Ext.Msg.alert('信息', '上传考核文件成功');
							me.close();
							
						}
					},
					failure  : function()
					{
						Ext.Msg.alert('错误', '上传考核文件失败');
					}
				});
			}}
	});
	
	Ext.Loader.setPath("org.jks", "view/common");
	/*
	 * 导入附件
	 * */
	function importAttachment(grid)
	{
		Ext.create('ImportWindow', {
		}).show();
	}
	
	function exportToExcel()
	{
		var data = grid.getStore();
		if(data.getCount == 0)
		{
			Ext.Msg.alert('错误', '没有任何数据');
			return;
		}
		
		window.open('/assess/export/' + grid.storeParams.type +'/' + grid.storeParams.startTime + '/' + grid.storeParams.endTime);
	}
});